package com.qf.utils;

import com.qf.pojo.TbUser;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class MaiUtils {
    public static void sendMail(TbUser user) throws MessagingException {
        Properties properties = new Properties();
        properties.setProperty("mail.host", "smtp.qq.com");
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(properties);
        session.setDebug(true);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("1461434806@qq.com"));
        message.setSubject("欢迎你");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
        message.setContent("欢迎你 http://localhost:8888/active?username="+user.getUsername()+"&code="+user.getCode(),"text/html;charset=utf-8");
        message.setSentDate(new Date());
        message.saveChanges();

        Transport transport = session.getTransport();
        transport.connect("1461434806@qq.com","rtposorhjbnkjjhh");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
